[tradeNo].tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. import { Image } from 'expo-image';
  2. import { useLocalSearchParams, useRouter } from 'expo-router';
  3. import React, { useCallback, useEffect, useState } from 'react';
  4. import {
  5. ActivityIndicator,
  6. Alert,
  7. ImageBackground,
  8. ScrollView,
  9. StatusBar,
  10. StyleSheet,
  11. Text,
  12. TouchableOpacity,
  13. View
  14. } from 'react-native';
  15. import { useSafeAreaInsets } from 'react-native-safe-area-context';
  16. import { Images } from '@/constants/images';
  17. import { confirmReceive, getOrderDetail, OrderDetail, payOrder } from '@/services/mall';
  18. export default function OrderDetailScreen() {
  19. const { tradeNo } = useLocalSearchParams<{ tradeNo: string }>();
  20. const router = useRouter();
  21. const insets = useSafeAreaInsets();
  22. const [loading, setLoading] = useState(true);
  23. const [data, setData] = useState<OrderDetail | null>(null);
  24. const loadData = useCallback(async () => {
  25. if (!tradeNo) return;
  26. setLoading(true);
  27. try {
  28. const detail = await getOrderDetail(tradeNo);
  29. setData(detail);
  30. } catch (error) {
  31. console.error('加载订单详情失败:', error);
  32. }
  33. setLoading(false);
  34. }, [tradeNo]);
  35. useEffect(() => {
  36. loadData();
  37. }, [loadData]);
  38. // 去支付
  39. const handlePay = async () => {
  40. if (!data) return;
  41. try {
  42. const result = await payOrder(data.tradeNo, 'ALIPAY');
  43. if (result?.paySuccess) {
  44. Alert.alert('成功', '支付成功');
  45. loadData();
  46. }
  47. } catch (error) {
  48. console.error('支付失败:', error);
  49. Alert.alert('错误', '支付失败');
  50. }
  51. };
  52. // 确认收货
  53. const handleReceive = () => {
  54. Alert.alert('提示', '确认已收到商品?', [
  55. { text: '取消', style: 'cancel' },
  56. {
  57. text: '确认',
  58. onPress: async () => {
  59. try {
  60. await confirmReceive(tradeNo!);
  61. Alert.alert('成功', '确认收货成功');
  62. loadData();
  63. } catch (error) {
  64. console.error('确认收货失败:', error);
  65. }
  66. },
  67. },
  68. ]);
  69. };
  70. const goBack = () => {
  71. router.back();
  72. };
  73. if (loading) {
  74. return (
  75. <View style={styles.loadingContainer}>
  76. <ActivityIndicator size="large" color="#fff" />
  77. </View>
  78. );
  79. }
  80. if (!data) {
  81. return (
  82. <View style={styles.loadingContainer}>
  83. <Text style={styles.errorText}>订单不存在</Text>
  84. </View>
  85. );
  86. }
  87. return (
  88. <View style={styles.container}>
  89. <StatusBar barStyle="light-content" />
  90. <ImageBackground
  91. source={{ uri: Images.mine.kaixinMineBg }}
  92. style={styles.background}
  93. resizeMode="cover"
  94. >
  95. {/* 顶部导航 */}
  96. <View style={[styles.header, { paddingTop: insets.top }]}>
  97. <TouchableOpacity style={styles.backBtn} onPress={goBack}>
  98. <Text style={styles.backText}>←</Text>
  99. </TouchableOpacity>
  100. <Text style={styles.headerTitle}>订单详情</Text>
  101. <View style={styles.placeholder} />
  102. </View>
  103. <ScrollView style={styles.scrollView} showsVerticalScrollIndicator={false}>
  104. {/* 订单状态 */}
  105. <View style={styles.statusSection}>
  106. <Text style={styles.statusText}>{data.statusText}</Text>
  107. </View>
  108. {/* 收货地址 */}
  109. {data.address && (
  110. <ImageBackground
  111. source={{ uri: Images.common.itemBg }}
  112. style={styles.section}
  113. resizeMode="stretch"
  114. >
  115. <Text style={styles.sectionTitle}>收货信息</Text>
  116. <View style={styles.addressInfo}>
  117. <Text style={styles.addressName}>
  118. {data.address.contactName} {data.address.contactNo}
  119. </Text>
  120. <Text style={styles.addressDetail}>
  121. {data.address.province}{data.address.city}{data.address.district}{data.address.address}
  122. </Text>
  123. </View>
  124. </ImageBackground>
  125. )}
  126. {/* 商品信息 */}
  127. <ImageBackground
  128. source={{ uri: Images.common.itemBg }}
  129. style={styles.section}
  130. resizeMode="stretch"
  131. >
  132. <Text style={styles.sectionTitle}>商品信息</Text>
  133. <View style={styles.goodsItem}>
  134. <Image source={data.goodsCover} style={styles.goodsImage} contentFit="cover" />
  135. <View style={styles.goodsInfo}>
  136. <Text style={styles.goodsName} numberOfLines={2}>{data.goodsName}</Text>
  137. <View style={styles.goodsBottom}>
  138. <Text style={styles.goodsPrice}>¥{data.paymentAmount}</Text>
  139. <Text style={styles.goodsQty}>x{data.quantity}</Text>
  140. </View>
  141. </View>
  142. </View>
  143. </ImageBackground>
  144. {/* 订单信息 */}
  145. <ImageBackground
  146. source={{ uri: Images.common.itemBg }}
  147. style={styles.section}
  148. resizeMode="stretch"
  149. >
  150. <Text style={styles.sectionTitle}>订单信息</Text>
  151. <View style={styles.infoRow}>
  152. <Text style={styles.infoLabel}>订单编号</Text>
  153. <Text style={styles.infoValue}>{data.tradeNo}</Text>
  154. </View>
  155. <View style={styles.infoRow}>
  156. <Text style={styles.infoLabel}>下单时间</Text>
  157. <Text style={styles.infoValue}>{data.createTime}</Text>
  158. </View>
  159. <View style={styles.infoRow}>
  160. <Text style={styles.infoLabel}>实付金额</Text>
  161. <Text style={[styles.infoValue, styles.priceText]}>¥{data.paymentAmount}</Text>
  162. </View>
  163. </ImageBackground>
  164. <View style={{ height: 100 }} />
  165. </ScrollView>
  166. {/* 底部操作栏 */}
  167. {(data.status === 1 || data.status === 3) && (
  168. <View style={[styles.bottomBar, { paddingBottom: insets.bottom + 10 }]}>
  169. {data.status === 1 && (
  170. <TouchableOpacity style={styles.payBtn} onPress={handlePay} activeOpacity={0.8}>
  171. <ImageBackground
  172. source={{ uri: Images.common.loginBtn }}
  173. style={styles.payBtnBg}
  174. resizeMode="stretch"
  175. >
  176. <Text style={styles.payBtnText}>去支付</Text>
  177. </ImageBackground>
  178. </TouchableOpacity>
  179. )}
  180. {data.status === 3 && (
  181. <TouchableOpacity style={styles.payBtn} onPress={handleReceive} activeOpacity={0.8}>
  182. <ImageBackground
  183. source={{ uri: Images.common.loginBtn }}
  184. style={styles.payBtnBg}
  185. resizeMode="stretch"
  186. >
  187. <Text style={styles.payBtnText}>确认收货</Text>
  188. </ImageBackground>
  189. </TouchableOpacity>
  190. )}
  191. </View>
  192. )}
  193. </ImageBackground>
  194. </View>
  195. );
  196. }
  197. const styles = StyleSheet.create({
  198. container: {
  199. flex: 1,
  200. backgroundColor: '#1a1a2e',
  201. },
  202. background: {
  203. flex: 1,
  204. },
  205. loadingContainer: {
  206. flex: 1,
  207. backgroundColor: '#1a1a2e',
  208. justifyContent: 'center',
  209. alignItems: 'center',
  210. },
  211. errorText: {
  212. color: '#999',
  213. fontSize: 16,
  214. },
  215. header: {
  216. flexDirection: 'row',
  217. alignItems: 'center',
  218. justifyContent: 'space-between',
  219. paddingHorizontal: 15,
  220. paddingBottom: 10,
  221. },
  222. backBtn: {
  223. width: 40,
  224. height: 40,
  225. justifyContent: 'center',
  226. alignItems: 'center',
  227. },
  228. backText: {
  229. color: '#fff',
  230. fontSize: 24,
  231. },
  232. headerTitle: {
  233. color: '#fff',
  234. fontSize: 18,
  235. fontWeight: '600',
  236. },
  237. placeholder: {
  238. width: 40,
  239. },
  240. scrollView: {
  241. flex: 1,
  242. },
  243. statusSection: {
  244. backgroundColor: '#ff6b00',
  245. padding: 20,
  246. marginHorizontal: 15,
  247. borderRadius: 12,
  248. alignItems: 'center',
  249. },
  250. statusText: {
  251. color: '#fff',
  252. fontSize: 18,
  253. fontWeight: '600',
  254. },
  255. section: {
  256. marginTop: 10,
  257. marginHorizontal: 15,
  258. padding: 15,
  259. borderRadius: 12,
  260. overflow: 'hidden',
  261. },
  262. sectionTitle: {
  263. color: '#333',
  264. fontSize: 15,
  265. fontWeight: '600',
  266. marginBottom: 12,
  267. },
  268. addressInfo: {},
  269. addressName: {
  270. color: '#333',
  271. fontSize: 15,
  272. fontWeight: '500',
  273. },
  274. addressDetail: {
  275. color: '#666',
  276. fontSize: 13,
  277. marginTop: 6,
  278. lineHeight: 18,
  279. },
  280. goodsItem: {
  281. flexDirection: 'row',
  282. },
  283. goodsImage: {
  284. width: 80,
  285. height: 80,
  286. borderRadius: 8,
  287. backgroundColor: '#fff',
  288. },
  289. goodsInfo: {
  290. flex: 1,
  291. marginLeft: 12,
  292. justifyContent: 'space-between',
  293. },
  294. goodsName: {
  295. color: '#333',
  296. fontSize: 14,
  297. lineHeight: 20,
  298. },
  299. goodsBottom: {
  300. flexDirection: 'row',
  301. justifyContent: 'space-between',
  302. alignItems: 'center',
  303. },
  304. goodsPrice: {
  305. color: '#ff6b00',
  306. fontSize: 16,
  307. fontWeight: '600',
  308. },
  309. goodsQty: {
  310. color: '#666',
  311. fontSize: 13,
  312. },
  313. infoRow: {
  314. flexDirection: 'row',
  315. justifyContent: 'space-between',
  316. paddingVertical: 8,
  317. },
  318. infoLabel: {
  319. color: '#666',
  320. fontSize: 13,
  321. },
  322. infoValue: {
  323. color: '#333',
  324. fontSize: 13,
  325. },
  326. priceText: {
  327. color: '#ff6b00',
  328. fontWeight: '600',
  329. },
  330. bottomBar: {
  331. position: 'absolute',
  332. bottom: 0,
  333. left: 0,
  334. right: 0,
  335. flexDirection: 'row',
  336. justifyContent: 'flex-end',
  337. paddingHorizontal: 15,
  338. paddingTop: 10,
  339. backgroundColor: 'rgba(0,0,0,0.3)',
  340. },
  341. payBtn: {
  342. width: 120,
  343. height: 45,
  344. overflow: 'hidden',
  345. },
  346. payBtnBg: {
  347. width: '100%',
  348. height: '100%',
  349. justifyContent: 'center',
  350. alignItems: 'center',
  351. },
  352. payBtnText: {
  353. color: '#fff',
  354. fontSize: 15,
  355. fontWeight: '600',
  356. },
  357. });